﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class FileSeparateAndCombine
{
    public const int PART_SIZE = 5120;

    public static void SeparateFile(string fileName)
    {
        FileStream fileReader = new FileStream(fileName, FileMode.Open);
        using (fileReader)
        {
            using(FileStream writeOutOne = new FileStream("out1",FileMode.CreateNew),
                writeOutTwo = new FileStream("out2",FileMode.CreateNew))
            {
                byte[] buff = new byte[PART_SIZE];
                char target = '1';
                while (true)
                {
                    int readBytes = fileReader.Read(buff, 0, buff.Length);
                    if (readBytes == 0)
                    {
                        break;
                    }
                    switch (target)
                    {
                        case '1':
                            writeOutOne.Write(buff, 0, readBytes);
                            target = '2';
                            break;
                        case '2':
                            writeOutTwo.Write(buff, 0, readBytes);
                            target = '1';
                            break;
                    }
                }
            }
        }
    }

    public static void CombineFile(string part1, string part2)
    {
        using(FileStream readOutOne = new FileStream(part1,FileMode.Open),
            readOutTwo = new FileStream(part2,FileMode.Open), combine = new FileStream("combine.jpg",FileMode.CreateNew))
        {
            byte[] buff = new byte[PART_SIZE];
            char source = '1';
            while (true)
            {
                int bytesRead = 0;
                switch (source)
                {
                    case '1':
                        bytesRead = readOutOne.Read(buff, 0, buff.Length);
                        source = '2';
                        break;
                    case '2':
                        bytesRead = readOutTwo.Read(buff, 0, buff.Length);
                        source = '1';
                        break;
                }
                if (bytesRead == 0)
                {
                    break;
                }
                combine.Write(buff, 0, bytesRead);
            }
        }
    }

    static void Main()
    {
        SeparateFile("mancho.jpg");
        CombineFile("out1", "out2");
    }
}
